﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using System.IO;
using DVK.SP2007.Constants;
using System.Diagnostics;

namespace DVK.SP2007
{
    /// <summary>
    /// Manages resx files under SharePoint root\resources. 
    /// Copies resx files under App_GlobalResources so that localizes strings would be available on the.
    /// </summary>
    public class GlobalResourcesManager
    {
        /// <summary>
        /// Resx files search pattern.
        /// </summary>
        public string FileSearchPattern = "Dvk*.resx";
        /// <summary>
        /// Name of the Resources folder under 12\Root.
        /// </summary>
        public string ResourcesFilepathPart = "Resources";
        /// <summary>
        /// Name of the App_GlobalResources folder under web app.
        /// </summary>
        public string GlobalResourcesFilepathPart = "App_GlobalResources";

        private SPSite _site;
        private Logger _logger;
        /// <summary>
        /// Initializes a new instance of the <see cref="GlobalResourcesManager"/> class.
        /// 
        /// <remarks>
        /// Logger is also created on initializing.
        /// </remarks>
        /// </summary>
        /// <param name="site">The site.</param>
        public GlobalResourcesManager(SPSite site)
        {
            _site = site;
            _logger = new Logger(LoggingSources.GlobalResourcesManager, SourceLevels.Information);
        }

        /// <summary>
        /// Copies the resx from 12\ROOT\RESOURCES to App_GlobalResources under web app.
        /// </summary>
        public void CopyResourcesUnderWebApp()
        {
            string sourcePath = string.Empty, destPath = string.Empty;
            try
            {
                SPWebApplication webApp = _site.WebApplication;

                foreach (SPUrlZone zone in webApp.IisSettings.Keys)
                {
                    // The settings of the IIS application to update     
                    SPIisSettings oSettings = webApp.IisSettings[zone];
                    // Determine the source and destination path     
                    sourcePath = SPUtility.GetGenericSetupPath(ResourcesFilepathPart);    
                    destPath = Path.Combine(oSettings.Path.ToString(), GlobalResourcesFilepathPart);

                    string[] filePaths = Directory.GetFiles(sourcePath, FileSearchPattern);
                    // Copy the files      
                    foreach (string filePath in filePaths)
                    {
                        string fileName = Path.GetFileName(filePath);
                        File.Copy(filePath, Path.Combine(destPath, fileName), true);
                    }

                    _logger.LogInformative("{0}: Found '{1}' RESX files. Source: {2}, Destination: {3}", _site.Url, filePaths.Length, sourcePath, destPath);
                }
            }
            catch (Exception exc)
            {
                _logger.LogError(
                    LoggerEvents.FailedToCopyResourcesToWebApp,
                    exc,
                    "{0}: Failed to copy resx files. Source: {1}, destination: {2}.",
                    _site.Url,
                    sourcePath,
                    destPath
                    );
            }
        }
    }
}
